package wangjg.coco.views;

import java.io.IOException;
import java.util.List;

import org.eclipse.core.resources.IResource;
import org.eclipse.jface.preference.PreferenceStore;
import org.eclipse.swt.SWT;
import org.eclipse.swt.custom.CCombo;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.DirectoryDialog;
import org.eclipse.swt.widgets.Event;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Listener;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Text;

import wangjg.coco.module.Source2SourceProcessor;
import wangjg.coco.widgets.FileTreeBox;

/**
 * 
 * @author wangjg
 *
 */
public class Source2SourceDialog extends AbstractDialog {
	private static final String PREFERENCE_STORE_PATH = Source2SourceDialog.class.getName();
	
//	private Label	modelInputLabel = null;
//	private Text	modelInputText = null;
//	private Button	modelInputButton = null;
	
	private Label	templateLabel = null;
	private Text	templateDirText = null;
	private Button	templateDirChoseButton = null;
	
	private Label	outputLabel = null;
	private Text	outputDirText = null;
	private Button	outputDirChoseButton = null;
	
	private Label	filterLabel = null;
	private Text	filterText = null;
	
	private CCombo 	genCombo = null;
	private Button	separateCheckBox = null;
	private Button	extendCheckBox = null;

	private Composite filePanel;
	private FileTreeBox treeBox;
	
	private List<IResource> resourceList;
	private String modelSource;

	public Source2SourceDialog(Shell parentShell) {
		super(parentShell);
		this.setShellStyle(this.getShellStyle()|SWT.RESIZE);
	}

	protected Control createDialogArea(Composite parent) {
		Composite composite = (Composite) super.createDialogArea(parent);
		this.setLayout(composite);
		
//		modelInputLabel = new Label(composite, SWT.NONE);
//		modelInputLabel.setText("模型");
//		modelInputText = new Text(composite, SWT.BORDER|SWT.READ_ONLY|SWT.WRAP|SWT.V_SCROLL);
//		this.modelInputText.setText(this.modelSource);
//		setGridData(modelInputText, 1);
//		modelInputButton = new Button(composite, SWT.NONE);
//		modelInputButton.setText("选择..");
//		modelInputButton.addSelectionListener(new SelectionAdapter() {
//			public void widgetSelected(SelectionEvent e) {
//				selectFilePath("model");
//			}
//		});
		/* 以上模型选择未完成 */
		
		templateLabel = new Label(composite, SWT.NONE);
		templateLabel.setText("模版文件夹");
		templateDirText = new Text(composite, SWT.BORDER);
		setGridData(templateDirText, 1);
		templateDirText.addListener(SWT.FocusOut, new Listener(){
			public void handleEvent(Event event) {
				setFileSelector();
			}
		});
		
		templateDirChoseButton = new Button(composite, SWT.NONE);
		templateDirChoseButton.setText("选择..");
		templateDirChoseButton.addSelectionListener(new SelectionAdapter() {
			public void widgetSelected(SelectionEvent e) {
				selectFilePath("in");
			}
		});
		
		outputLabel = new Label(composite, SWT.NONE);
		outputLabel.setText("输出文件夹");
		outputDirText = new Text(composite, SWT.BORDER);
		setGridData(outputDirText, 1);
		outputDirChoseButton = new Button(composite, SWT.NONE);
		outputDirChoseButton.setText("选择..");
		outputDirChoseButton.addSelectionListener(new SelectionAdapter() {
			public void widgetSelected(SelectionEvent e) {
				selectFilePath("out");
			}
		});
		
		filterLabel = new Label(composite, SWT.NONE);
		filterLabel.setText("过滤模版");
		filterText = new Text(composite, SWT.BORDER);
		filterText.setText("*");
		setGridData(filterText, 1);
		new Label(composite, SWT.NONE);//空白站位
		
//		extendCheckBox = new Button(composite, SWT.CHECK);
//		extendCheckBox.setText("继承");
		new Label(composite, SWT.NONE);//空白站位

		genCombo = new CCombo(composite, SWT.BORDER|SWT.READ_ONLY);
		genCombo.add("");
		
		separateCheckBox = new Button(composite, SWT.CHECK);
		separateCheckBox.setText("分别生成");
		
//		String[] items = new String[]{"aaa","bbb","cccc","ddd","ee"};
//		org.eclipse.swt.widgets.List multiList = new org.eclipse.swt.widgets.List(
//				composite, SWT.BORDER | SWT.MULTI | SWT.V_SCROLL);
//		multiList.setItems(items);
//		multiList.select(5, 7);
		//!!!
//		filePanel = new Composite(composite, SWT.NONE);
//		setGridData(filePanel, 3);
		
		treeBox = new FileTreeBox(composite, SWT.NONE, true, null);
		setGridData(treeBox, 2);
		
		this.loadSetting();

		this.setFileSelector();

		return composite;
	}

	private void setLayout(Composite composite){
		setGridLayout(composite, 3);
	}
	
	private void setFileSelector(){
		String path = templateDirText.getText();
		String _root = treeBox.getRoot();
		if(_root!=null && _root.equals(path)){
			return;
		}
		treeBox.load(path);
		treeBox.setCheckedAll(true);
		treeBox.setExpandedAll(false);
		treeBox.getTree().setSize(430, 220);
	}

	/*
	 * 文件夹（目录）选择对话框
	 */
	private void selectFilePath(String type) {
		if("in".equals(type)){
			String dir = selectDialog(templateDirText.getText());
			if(dir!=null){
				templateDirText.setText(dir);
				setFileSelector();
			}
		}else if("out".equals(type)){
			String dir = selectDialog(outputDirText.getText());
			if(dir!=null){
				outputDirText.setText(dir);
			}
		}else if("model".equals(type)){
//			String dir = selectDialog(outputDirText.getText());
//			if(dir!=null){
//				modelInputText.setText(dir);
//			}
		}
	}
	
	private String selectDialog(String path){
		DirectoryDialog folderdlg = new DirectoryDialog(this.getShell());
		folderdlg.setText("选择文件");
		folderdlg.setFilterPath(path);
		String selectedDir = folderdlg.open();
		return selectedDir;
	}

	protected void okPressed(){
		Source2SourceProcessor mp = new Source2SourceProcessor();
		mp.setResourceList(resourceList);
		mp.setTemplateDir(templateDirText.getText());
		mp.setTargetDir(outputDirText.getText());
		mp.setSeparate(separateCheckBox.getSelection());
		mp.setFilterExt(filterText.getText());
		if(treeBox!=null){
			mp.setTemplSelection(treeBox.getSelectedFiles());
		}
		mp.setPreprocessor(PreprocessorFactory.get(resourceList.get(0)));
		mp.process();
		this.saveSetting();
		super.okPressed();
	}
	
	private void saveSetting(){
		PreferenceStore preferenceStore = new PreferenceStore(PREFERENCE_STORE_PATH);
		preferenceStore.setValue("templateDir", this.templateDirText.getText());
		preferenceStore.setValue("outputDir", this.outputDirText.getText());
		preferenceStore.setValue("filter", this.filterText.getText());
		preferenceStore.setValue("separate", this.separateCheckBox.getSelection());
		try {
			preferenceStore.save();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	
	private void loadSetting(){
		PreferenceStore preferenceStore = new PreferenceStore(PREFERENCE_STORE_PATH);
		try {
			preferenceStore.load();
		} catch (IOException e) {
			e.printStackTrace();
			return ;
		}
		this.templateDirText.setText(preferenceStore.getString("templateDir"));
		this.outputDirText.setText(preferenceStore.getString("outputDir"));
		this.filterText.setText(preferenceStore.getString("filter"));
		this.separateCheckBox.setSelection(preferenceStore.getBoolean("separate"));
	}

	public void setResourceList(List<IResource> pathList) {
		this.resourceList = pathList;
	}
	
	public void setModelPath(String path) {
		this.modelSource = path;
	}

	protected int getShellStyle(){
        return super.getShellStyle();//|SWT.RESIZE|SWT.MAX;
    }
}